package interfaces.menu.joinGame.gamesTable;

import interfaces.menu.joinGame.gamesTable.GamesTable.Column;

import java.util.Comparator;

public class GamesTableComparator implements Comparator<GamesTableWidget> {
	
	private Column column;
	private boolean ascending;
	
	public GamesTableComparator(Column column, boolean ascending) {
		this.column = column;
		this.ascending = ascending;
	}
	
	@Override
	public int compare(GamesTableWidget w1, GamesTableWidget w2) {
		int ascendingValue = 0;
		switch(column) {
			case ServerName: ascendingValue = w1.getServerName().compareTo(w2.getServerName()); break;
			case CurrentMap: ascendingValue = w1.getCurrentMap().compareTo(w2.getCurrentMap()); break;
			case GameMode: ascendingValue = w1.getGameMode().toString().compareTo(w2.getGameMode().toString()); break;
			case Players: ascendingValue = compareInts(w1.getPlayers().size(), w2.getPlayers().size()); break;
			case Ping: ascendingValue = compareInts(w1.getPing(), w2.getPing()); break;
		}
		if(!ascending) ascendingValue *= -1;
		return ascendingValue;
	}
	
	private int compareInts(int v1, int v2) { return new Integer(v1).compareTo(new Integer(v2)); }
}